//
//  CYAboutUsViewController.m
//  cyjf
//
//  Created by tauCross on 2017/8/28.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYAboutUsViewController.h"

#import "CYAboutUsCell.h"
#import "CYEmptyCell.h"

#import "CYFeedbackViewController.h"
#import "CYWebViewController.h"

#import <StoreKit/StoreKit.h>



@interface CYAboutUsViewController ()

@property(nonatomic, strong)CYAboutUsCellObject *aboutCell;
@property(nonatomic, strong)CYAboutUsCellObject *advantageCell;
@property(nonatomic, strong)CYAboutUsCellObject *safeCell;
@property(nonatomic, strong)CYAboutUsCellObject *feedbackCell;
@property(nonatomic, strong)CYAboutUsCellObject *rateCell;

@end


@implementation CYAboutUsViewController

- (void)loadUI
{
    self.navigationItem.title = @"关于我们";
    self.tableView.cy_autoLockVertical = YES;
    UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    {
        UIImageView *iconImageView = [[UIImageView alloc] initWithImage:CYImage(@"about_us_icon")];
        iconImageView.centerX = tableHeaderView.width / 2;
        iconImageView.top = 50;
        [tableHeaderView addSubview:iconImageView];
        
        UILabel *versionLabel = [[UILabel alloc] init];
        versionLabel.text = [NSString stringWithFormat:@"版本号 %@", CYAppVersion];
        versionLabel.textColor = HEXCOLOR(@"888888");
        versionLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        [versionLabel sizeToFit];
        versionLabel.centerX = tableHeaderView.width / 2;
        versionLabel.centerY = 140;
        [tableHeaderView addSubview:versionLabel];
    }
    self.tableView.tableHeaderView = tableHeaderView;
    [self loadCell];
}

- (void)loadCell
{
    NSMutableArray *cells = [@[] mutableCopy];
    {
        self.aboutCell = [CYAboutUsCellObject objectWithCellClass:[CYAboutUsCell class]];
        self.aboutCell.title = @"了解车蚁金服";
        [cells addObject:self.aboutCell];
    }
    {
        self.advantageCell = [CYAboutUsCellObject objectWithCellClass:[CYAboutUsCell class]];
        self.advantageCell.title = @"我们的优势";
        [cells addObject:self.advantageCell];
    }
    {
        self.safeCell = [CYAboutUsCellObject objectWithCellClass:[CYAboutUsCell class]];
        self.safeCell.title = @"安全保障";
        self.safeCell.isBottom = YES;
        [cells addObject:self.safeCell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    {
        self.feedbackCell = [CYAboutUsCellObject objectWithCellClass:[CYAboutUsCell class]];
        self.feedbackCell.title = @"意见与反馈";
        [cells addObject:self.feedbackCell];
    }
    {
        self.rateCell = [CYAboutUsCellObject objectWithCellClass:[CYAboutUsCell class]];
        self.rateCell.title = @"给我们评分";
        self.rateCell.isBottom = YES;
        [cells addObject:self.rateCell];
    }
    
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
}

#pragma mark - Actions
- (void)rateAction
{
    NSString *url = [NSString  stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review", CY_APP_STORE_ID];
    [[UIApplication sharedApplication] openURL:url.tc_url];
}

- (void)feedbackAction
{
    CYFeedbackViewController *feedbackViewController = [[CYFeedbackViewController alloc] init];
    [self.navigationController pushViewController:feedbackViewController animated:YES];
}

- (void)aboutAction
{
    CYWebViewController *webViewController = [[CYWebViewController alloc] init];
    webViewController.url = [CY_H5_BASE_URL stringByAppendingString:@"views/know_us.html"];
    [self.navigationController pushViewController:webViewController animated:YES];
}

- (void)advantageAction
{
    CYWebViewController *webViewController = [[CYWebViewController alloc] init];
    webViewController.url = [CY_H5_BASE_URL stringByAppendingString:@"views/why_choice.html"];
    [self.navigationController pushViewController:webViewController animated:YES];
}

- (void)safeAction
{
    CYWebViewController *webViewController = [[CYWebViewController alloc] init];
    webViewController.url = [CY_H5_BASE_URL stringByAppendingString:@"views/safe.html"];
    [self.navigationController pushViewController:webViewController animated:YES];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NICellObject *cellObject = [self.tableModel objectAtIndexPath:indexPath];
    if(cellObject == self.rateCell)
    {
        [self rateAction];
        return;
    }
    if(cellObject == self.feedbackCell)
    {
        [self feedbackAction];
        return;
    }
    if(cellObject == self.aboutCell)
    {
        [self aboutAction];
        return;
    }
    if(cellObject == self.advantageCell)
    {
        [self advantageAction];
        return;
    }
    if(cellObject == self.safeCell)
    {
        [self safeAction];
        return;
    }
}

@end
